package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.BaseEntity;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingPlanProject;
import com.zzyl.entity.NursingProject;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingPlanProjectMapper;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingPlanProjectVo;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 护理项目Service实现类
 */
@Service
public class NursingProjectServiceImpl implements NursingProjectService {

    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    @Autowired
    private NursingPlanProjectMapper nursingPlanProjectMapper;

    @Override
    public PageResponse<NursingProjectVo> getByPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingProject> nursingProjects = nursingProjectMapper.selectByPage(name, status);
        PageResponse<NursingProjectVo> projectVoPageResponse = PageResponse.of(nursingProjects, NursingProjectVo.class);
        return projectVoPageResponse;
    }

    @Override
    public Boolean insert(NursingProjectDto dto) {

        NursingProject nursingProject = BeanUtil.toBean(dto,NursingProject.class);
        Integer resout = nursingProjectMapper.insert(nursingProject);
        return resout>0;
    }

    @Override
    public NursingProjectVo selectById(Long id) {
        NursingProjectVo vo = nursingProjectMapper.selectById(id);
        return vo;
    }

    @Override
    public Boolean update(NursingProjectDto dto) {
        NursingProject bo = BeanUtil.toBean(dto,NursingProject.class);
        Integer resout = nursingProjectMapper.update(bo);
        return resout>0;
    }

    @Override
    public Boolean delete(Long id) {
        NursingPlanProject nursingPlanProject = new NursingPlanProject();
        nursingPlanProject.setProjectId(id);
        List<NursingPlanProjectVo> list = nursingPlanProjectMapper.list(nursingPlanProject);
        if (!list.isEmpty()){
            throw new BaseException(BasicEnum.NURSINGPROJECT_DELETE_FAIL);
        }
        Integer del = nursingProjectMapper.delete(id);
        return del>0;
    }

    @Override
    public Boolean changeStatus(Long id, Integer status) {
        NursingProject vo = new NursingProject();
        vo.setId(id);
        vo.setStatus(status);
        Integer update = nursingProjectMapper.update(vo);
        return update>0;
    }

    @Override
    public List<NursingProjectVo> list() {
        List<NursingProject> list = nursingProjectMapper.list();
        List<NursingProjectVo> resout = BeanUtil.copyToList(list,NursingProjectVo.class);
        return resout;
    }


}